﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;

namespace IronOne.SalesOne.SalesOneWeb
{
    public partial class AddBranch : System.Web.UI.Page
    {
        //private String action = "Create";
        //private String action = "Update";
        //private String action = "Delete";
        private String action="View";
        int successDel = 0;
        int successUpd = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (action == "Create")
            {
                lblHeader.Text = "Create Branch";
                btnDone.Text = "Create";
            }
            else if (action == "Update")
            {
                lblHeader.Text = "Update Branch";
                btnDone.Text = "Update";

                    string branchStr = Session["/Structure/ManageBranches.aspxview"].ToString();
                    int branchID = int.Parse(branchStr);
                    StructureFacade branchFecade = new StructureFacade();
                    Branch branchObject = new Branch();
                    branchObject= branchFecade.GetBranch(branchID);
                    
                    //branchFecade.UpdateBranch(branchObject);

                    //GroupDropDownList.Text = branchObject.GroupName;
                    txtCode.Text = branchObject.BranchCode;
                    txtName.Text = branchObject.BranchName;
                    txtAdress1.Text = branchObject.Address1;
                    txtAddress2.Text = branchObject.Address2;
                    CountryDropDownList.Text = branchObject.Country;
                    txtCity.Text = branchObject.City;
                    txtPostalCode.Text = branchObject.PostalCode.ToString();
                    txtContactNo.Text = branchObject.ContactNo;
                    txtEffectiveFrom.Text = branchObject.EffectiveFrom.ToString();
                    txtEffectiveTo.Text = branchObject.EffectiveTo.ToString();


            }
            else if (action == "View")
            {
                lblHeader.Text = "View Branch";
                CountryDropDownList.Visible = false;
                GroupDropDownList.Visible = false;
                btnDone.Visible = false;
                txtCode.Visible = false;
                txtName.Visible = false;
                txtAdress1.Visible = false;
                txtAddress2.Visible = false;
                txtCity.Visible = false;
                txtPostalCode.Visible = false;
                txtContactNo.Visible = false;
                txtEffectiveFrom.Visible = false;
                txtEffectiveTo.Visible = false;

                string branchStr = Session["/Structure/ManageBranches.aspxview"].ToString();
                int branchID = int.Parse(branchStr);
                Branch br = new Branch();
                StructureFacade sf = new StructureFacade();
                br = sf.GetBranch(branchID);


                lblGroup.Text = br.GroupName;
                lblCode.Text = br.BranchCode;
                lblName.Text = br.BranchName;
                lblAddress1.Text = br.Address1;
                lblAddress2.Text = br.Address2;
                lblCountry.Text = br.Country;
                lblCity.Text = br.City;
                lblPostalCode.Text = br.PostalCode.ToString();
                lblContactNo.Text = br.ContactNo;
                lblEffectiveFrom.Text = br.EffectiveFrom.ToString();
                lblEffectiveTo.Text = br.EffectiveTo.ToString();
                 
                

            }
            else if (action == "Delete")
            {

                string branchStr = Session["/Structure/ManageBranches.aspxview"].ToString();
                int branchID = int.Parse(branchStr);
                Branch branchObject = new Branch();
                StructureFacade strFacade = new StructureFacade();
                branchObject=strFacade.GetBranch(branchID);
                successDel=strFacade.DeleteBranch(branchObject);

                if (successDel == 1)
                {
                    Response.Redirect("~/Structure/ManageBranches.aspx");
                }
                
            }


            //Page.Request.Params["gpname"];
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (action == "Create")
            {
                Branch branch = new Branch();


                branch.BranchCode = txtCode.Text;
                branch.BranchName = txtName.Text;
                branch.Address1 = txtAdress1.Text;
                branch.Address2 = txtAddress2.Text;
                
                branch.GroupId = int.Parse(GroupDropDownList.SelectedValue);
                //branch.GroupName = GroupDropDownList.SelectedValue;                
                branch.Country = CountryDropDownList.SelectedValue;
                branch.City = txtCity.Text;
                branch.PostalCode = Convert.ToInt32(txtPostalCode.Text);
                branch.ContactNo = txtContactNo.Text;
                branch.EffectiveFrom = DateTime.Parse(txtEffectiveFrom.Text);
                branch.EffectiveTo = DateTime.Parse(txtEffectiveTo.Text);

                StructureFacade branchFecade = new StructureFacade();
                branchFecade.InsertBranch(branch);


            }
            else if (action == "Update")
            {
                
                string branchStr = Session["/Structure/ManageBranches.aspxview"].ToString();
                int branchID = int.Parse(branchStr);
                Branch br = new Branch();
                StructureFacade sf = new StructureFacade();
                br = sf.GetBranch(branchID);
                successUpd = sf.UpdateBranch(br);

                if (successUpd == 1)
                {
                    Response.Redirect("~/Structure/ManageBranches.aspx");
                }
                else 
                {
                    txtAddress2.Text = successUpd.ToString();
                }
                 
                /*
                Branch branch = new Branch();


                branch.BranchCode = txtCode.Text;
                branch.BranchName = txtName.Text;
                branch.Address1 = txtAdress1.Text;
                branch.Address2 = txtAddress2.Text;

                branch.GroupId = int.Parse(GroupDropDownList.SelectedValue);
                //branch.GroupName = GroupDropDownList.SelectedValue;                
                branch.Country = CountryDropDownList.SelectedValue;
                branch.City = txtCity.Text;
                branch.PostalCode = Convert.ToInt32(txtPostalCode.Text);
                branch.ContactNo = txtContactNo.Text;
                branch.EffectiveFrom = DateTime.Parse(txtEffectiveFrom.Text);
                branch.EffectiveTo = DateTime.Parse(txtEffectiveTo.Text);

                StructureFacade branchFecade = new StructureFacade();
                branchFecade.UpdateBranch(branch);
                 * 
                 * 
                 * */


            }
            else if (action == "View")
            {
                
            }
        }

        

       
        
    }
}
